<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin/home/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>装备管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <el-form :inline="true" class="searchForm">
      <el-form-item>
        <el-input v-model="keyword" clearable placeholder="请输入要搜索的关键字" @clear="clear"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="add" plain icon="el-icon-plus">添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="dels" plain icon="el-icon-close" v-show="select.length!=0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <!-- 表单 -->
    <el-table :data="dataList" border stripe @selection-change="selectionChange" v-loading="loading" height="550px">
      <el-table-column type="selection"></el-table-column>
      <el-table-column label="名称" prop="name" min-width="150px" align="center"></el-table-column>
      <el-table-column label="数量" prop="num" min-width="50px" align="center"></el-table-column>
      <el-table-column label="单位" prop="unit" min-width="50px" align="center"></el-table-column>
      <el-table-column label="备注" prop="comment" align="center" width="200px" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" min-width="150px" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="edit(scope.row)" icon="el-icon-s-tools">编辑</el-button>
          <el-button size="mini" type="danger" @click="remove(scope.row)" icon="el-icon-close">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination :current-page="page.page" :total="page.total" background layout="prev, pager, next" @current-change="currentChange">
    </el-pagination>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑" :visible.sync="dialogEdit" center v-loading="loading">
      <el-form class="form" :model="form" label-left label-width="100px" :rules="rules" ref="editForm">
        <el-form-item label="名称" prop="name">
          <el-autocomplete class="inline-input" v-model="form.name" :fetch-suggestions="nameQuerySearch" placeholder="请输入名称"></el-autocomplete>
        </el-form-item>

        <el-form-item label="数目" prop="num">
          <el-input-number v-model.number="form.num" :min="0" :max="999"></el-input-number>
        </el-form-item>

        <el-form-item label="单位" prop="unit">
          <el-autocomplete class="inline-input" v-model="form.unit" :fetch-suggestions="unitQuerySearch" placeholder="请输入单位"></el-autocomplete>
        </el-form-item>

        <el-form-item label="备注" prop="comment">
          <el-input type="textarea" :rows="2" v-model="form.comment" placeholder="" :maxlength="-1" resize="none" :show-word-limit="false" :autosize="{ minRows: 2, maxRows: 4 }">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="doEdit('editForm')">确 定</el-button>
        <el-button @click="dialogEdit = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加对话框 -->
    <el-dialog title="新增" :visible.sync="dialogAdd" center v-loading="loading">
      <el-form class="form" :model="form" label-left label-width="100px" :rules="rules" ref="addForm">
        <el-form-item label="名称" prop="name">
          <el-autocomplete class="inline-input" v-model="form.name" :fetch-suggestions="nameQuerySearch" placeholder="请输入名称"></el-autocomplete>
        </el-form-item>

        <el-form-item label="数目" prop="num">
          <el-input-number v-model.number="form.num" :min="0" :max="999"></el-input-number>
        </el-form-item>

        <el-form-item label="单位" prop="unit">
          <el-autocomplete class="inline-input" v-model="form.unit" :fetch-suggestions="unitQuerySearch" placeholder="请输入单位"></el-autocomplete>
        </el-form-item>

        <el-form-item label="备注" prop="comment">
          <el-input type="textarea" :rows="2" v-model="form.comment" placeholder="" :maxlength="-1" resize="none" :show-word-limit="false" :autosize="{ minRows: 2, maxRows: 4 }">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="doAdd('addForm')">确 定</el-button>
        <el-button @click="dialogAdd = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  adminEquipmentPageApi as getListApi,
  adminEditEquipmentApi as editApi,
  adminAddEquipmentApi as addApi,
  adminDelEquipmentApi as delApi,
  adminDelsEquipmentApi as delsApi,
} from "@/api/admin.js";
export default {
  inject: ["reload"],
  data() {
    return {
      //表格展示数据
      dataList: [],
      //分页数据
      page: {
        page: 1,
        limit: 10,
        total: 0,
      },
      //选择的数据
      select: [],
      //加载
      loading: false,
      //表单数据
      form: {
        num: null,
      }, //表单数据
      keyword: "", //关键字
      dialogEdit: false, //显示编辑对话款
      dialogAdd: false, //显示编辑对话款
      //表单校验
      rules: {
        name: {
          required: true,
          message: "请输入名称",
          trigger: "blur",
        },
        num: {
          required: true,
          message: "请输入数目",
          trigger: "blur",
        },

        unit: {
          required: true,
          message: "请输入单位",
          trigger: "blur",
        },
      },
    };
  },
  methods: {
    //获取数据列表
    getList(
      page = this.page.page,
      limit = this.page.limit,
      keyword = this.keyword
    ) {
      let queryParam = {
        page,
        limit,
        keyword,
      };
      getListApi(queryParam)
        .then((res) => {
          this.loading = true;
          const result = res.data;
          if (result.status === 200) {
            const dataList = result.data.content;
            this.dataList = dataList;
            this.page.total = result.data.total;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //选择
    selectionChange(e) {
      this.select = e;
    },
    //分页
    currentChange(e) {
      this.page.page = e;
      this.getList();
    },
    //打开编辑对话款
    edit(e) {
      this.form = e;
      this.dialogEdit = true;
    },
    //执行编辑操作
    doEdit(e) {
      this.$refs[e].validate((valid) => {
        if (valid) {
          this.form.status = this.form.status ? 1 : 0;
          editApi(this.form)
            .then((res) => {
              this.loading = true;
              const result = res.data;
              if (result.status == 200) {
                this.$message.success("修改成功");
              } else {
                this.$message.error(result.msg);
              }
            })
            .finally(() => {
              this.loading = false;
              this.dialogEdit = false;
              this.reload();
            });
        }
      });
    },
    //打开添加对话款
    add() {
      this.form = {};
      this.form.status = true;
      this.dialogAdd = true;
    },
    //执行添加操作
    doAdd(e) {
      this.$refs[e].validate((valid) => {
        if (valid) {
          this.form.status = this.form.status ? 1 : 0;
          addApi(this.form)
            .then((res) => {
              this.loading = true;
              const result = res.data;
              if (result.status == 200) {
                this.$message.success("添加成功");
              } else {
                this.$message.error(result.msg);
              }
            })
            .finally(() => {
              this.loading = false;
              this.dialogAdd = false;
              this.reload();
            });
        }
      });
    },
    //删除
    remove(e) {
      this.$confirm("确认要删除吗？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          delApi({ id: e.id })
            .then((res) => {
              const result = res.data;
              if (result.status == 200) {
                this.$message.success("删除成功");
              } else {
                this.$message.error(result.msg);
              }
            })
            .finally(() => {
              this.reload();
            });
        })
        .catch(() => {
          return;
        });
    },
    //批量删除
    dels() {
      const ids = this.select.map((role) => {
        return role.id;
      });
      const data = { ids };
      this.$confirm("确认要删除吗？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          delsApi(data)
            .then((res) => {
              const result = res.data;
              if (result.status == 200) {
                this.$message.success("删除成功");
              } else {
                this.$message.error(result.msg);
              }
            })
            .finally(() => {
              this.reload();
            });
        })
        .catch(() => {
          return;
        });
    },
    //搜索
    search() {
      this.getList();
    },
    //清空文本框
    clear() {
      this.getList();
    },
    nameQuerySearch(str, cb) {
      var list = this.dataList.map((data) => {
        return data.name;
      });
      cb(this.uniqArr(list));
    },
    unitQuerySearch(str, cb) {
      var list = this.dataList.map((data) => {
        return data.unit;
      });
      cb(this.uniqArr(list));
    },
    uniqArr(list) {
      list = Array.from(new Set(list));
      list = list.map((str) => {
        return { value: str };
      });
      return list;
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style  scoped>
.el-tabs {
  margin-top: 50px;
}
.el-pagination {
  float: right;
  margin: 20px;
}
.searchForm {
  margin-left: 100px;
  margin-top: 20px;
}
.form {
  padding: 20px 50px;
}
.form .el-form-item {
  width: 300px;
  margin: 20px auto;
}
</style>